

#include "MyPlayerActionManager.h"
#include "MyUniversal.h"
#include "MyGame.h"
#include "MyEngine.h"
#include "MyPlayer.h"

MyPlayerActionManager::MyPlayerActionManager(MyGame* _this){parent = _this;}
MyPlayerActionManager::~MyPlayerActionManager()
{
	unload_surface(actionImgSheet);
	for (int i = 0; i < ST_SHOT_COUNT + ST_SPELL_COUNT; i++)
		delete(action_array[i]);
}
void MyPlayerActionManager::init()
{
	#if defined(_WIN32)
	actionImgSheet = load_image("misc\\action_images.png");
	#else
	actionImgSheet = load_image("misc/action_images.png");
	#endif
	
    //If there was a problem in loading the sheets
	if (!actionImgSheet)
	{	
		printf("ERROR:Cannot load game images! (check 'action_images.png')");
		parent->demand_quit();
	}	    
	actSpellPos = -1;
	actShotPos = ST_STD_BOW;
	
	//init of arrays holding positions, minus sign -> not enabled
	//shot
	action_array[ST_STD_BOW]	= new actionType(ST_STD_BOW,	1,2,0, 0, 1);
	action_array[ST_FIRE_BOW]	= new actionType(ST_FIRE_BOW,	1,6,1, 0, 0);  
	action_array[ST_ICE_BOW]	= new actionType(ST_ICE_BOW,	4,4,0, 0, 0);
	action_array[ST_HIT]		= new actionType(ST_HIT,		0,0,0, 0, 0);
	action_array[ST_FIREBALL]	= new actionType(ST_FIREBALL,	1,4,1, 1, 0);
	action_array[ST_FROSTBALL]	= new actionType(ST_FROSTBALL,	1,6,3, 1, 0);
	action_array[ST_LIGHTBALL]	= new actionType(ST_LIGHTBALL,	1,30,0, 1, 0);
}
bool MyPlayerActionManager::is_enabled(int _pos)
{
	return action_array[_pos]->enabled;
}
SDL_Rect* MyPlayerActionManager::getActShotRect()
{
	return this->getActionRect(actShotPos);
}
SDL_Rect* MyPlayerActionManager::getActSpellRect()
{
	if (actSpellPos == -1) return NULL; //no spells yet
	return this->getActionRect(actSpellPos);
}
//cycling through the player actions
void MyPlayerActionManager::setNextSpell()
{
	if (actSpellPos == -1) return; //no spells yet
		
	while (true) //until we set the new actSpellPos
	{
		if (actSpellPos + 1 == ACTION_ARRAY_SIZE) //jump to first
			actSpellPos = 0; //cycle
		else
		{			
			actSpellPos++;
			if ((action_array[actSpellPos]->spell) && (action_array[actSpellPos]->enabled)) //available spell
				break;
			else
				continue;
		}
	}
}
void MyPlayerActionManager::setNextShot()
{
	while (true) //until we set the new actShotPos
	{
		if (actShotPos + 1 == ACTION_ARRAY_SIZE) //jump to first
		{
			actShotPos = 0; //cycle
			break; //always allow
		}
		else
		{			
			actShotPos++;
			if (!action_array[actShotPos]->spell) //shot
				if (action_array[actShotPos]->enabled) //available
					break; //use it
			continue;
		}
	}
}
//returning rectangles for spells/shots
SDL_Rect* MyPlayerActionManager::getActionRect(int _pos)
{
	return make_rect(_pos*40, 0, 40, 40);
}
//returning positions of act spell/shot
int MyPlayerActionManager::getActSpellPos(){ return actSpellPos; }
int MyPlayerActionManager::getActShotPos(){ return actShotPos; }
int MyPlayerActionManager::getActShotDmg(){ return getDmg(action_array[actShotPos]->dice_times, action_array[actShotPos]->dice_size, action_array[actShotPos]->dice_add); }
int MyPlayerActionManager::getActSpellDmg(){ return getDmg(action_array[actSpellPos]->dice_times, action_array[actSpellPos]->dice_size, action_array[actSpellPos]->dice_add); }
bool MyPlayerActionManager::enableItem(int _pos) 
{ 
	if (_pos >= ACTION_ARRAY_SIZE) return false; //error	

	if ((action_array[_pos]->spell)&&(actSpellPos==-1)) //no spells before
	{
		actSpellPos = _pos;
		action_array[_pos]->enabled = true;
		return false;
	}
	if (action_array[_pos]->enabled) { return true; }
	else { action_array[_pos]->enabled = true; return false; }

}